<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Canvas缩放</title>
  <link rel="stylesheet" href="../../style/reset.css">
  <style>
    #canvasWrapper {
      width: 100vw;
      height: 100vh;
      overflow: hidden;
    }
  </style>
</head>

<body>
  <div id="canvasWrapper">
    <canvas id="canvasEle">您的浏览器不支持canvas</canvas>
  </div>
  <script>
    window.onload = () => {
      const canvasWrapper = document.getElementById('canvasWrapper');
      const canvasEle = document.getElementById('canvasEle');
      const ctx = canvasEle.getContext("2d");
      const image = new Image()
      let width = 0
      let height = 0

      image.src = './image/big.jpg'
      image.onload = () => {
        drawImage()
      }

      const drawImage = () => {
        width = canvasWrapper.clientWidth;
        height = canvasWrapper.clientHeight;
        canvasEle.width = width
        canvasEle.height = height
        ctx.drawImage(image, 0, 0);
      }

      const resizeCallBack = () => {
        drawImage()
      }

      window.addEventListener('resize', resizeCallBack)
    }
  </script>
</body>

</html>